package com.sanjiahantang.ythshopping.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.GoodsFilterActivity;
import com.sanjiahantang.ythshopping.activity.MainActivity;
import com.sanjiahantang.ythshopping.activity.me.JsWebActivity;
import com.sanjiahantang.ythshopping.activity.me.WebActivity;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.model.BaseModel;
import com.sanjiahantang.ythshopping.model.Menu;
import com.sanjiahantang.ythshopping.presenter.CheckPresenter;
import com.sanjiahantang.ythshopping.views.CheckView;
import com.sanjiahantang.shopping.uilibrary.uiutils.Utils;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/6/8.
 * 专题列表适配器
 */

public class GvSubjectAdapter extends BaseAdapter implements CheckView{
    private Context context;
    private LayoutInflater inflater;
    private List<Menu> datas;
    MainActivity activity;
    private int type ;
    public static final int TYPE_HOME = 0x001;
    public static final int TYPE_NEAR = 0x002;


    public GvSubjectAdapter(Context context, List<Menu> datas, MainActivity activity,int type){
        this.context=context;
        this.datas = datas;
        this.activity = activity;
        inflater=LayoutInflater.from(context);
        this.type = type;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view=inflater.inflate(R.layout.gv_item_subject,null);
            holder=new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }

       holder.bindData(context, ConfigValue.IMG_IP + datas.get(i).getMenu_img(), datas.get(i).getMenu_name());

        //分类
        String url = datas.get(i).getMenu_url();
        String lowerUrl = url.toLowerCase();



        if(lowerUrl.contains("http://") || lowerUrl.contains("https://")){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url="";
                    url = datas.get(i).getMenu_url();
                    Intent intentjs=new Intent(context, JsWebActivity.class);
                    if(url.indexOf("?")>0)
                    {
                        url= ConfigValue.IMG_IP+datas.get(i).getMenu_url()+"&from_app=2&key="+ConfigValue.DATA_KEY  + "&rid=" + ConfigValue.rid;
                    }else {
                        url= ConfigValue.IMG_IP+datas.get(i).getMenu_url()+"?from_app=2&key="+ConfigValue.DATA_KEY  + "&rid=" + ConfigValue.rid;
                    }
                    intentjs.putExtra("url", url);
                    intentjs.putExtra("index", 0);
                    context.startActivity(intentjs);
                }
            });
        }else if (lowerUrl.contains("/catalog.php")){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.setFragmentIndex(1);
                }
            });

        }else if (lowerUrl.contains("/index.php") || lowerUrl.endsWith("mobile/") || lowerUrl.contains("mobile/?")){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.setFragmentIndex(0);
                }
            });
        }else if (lowerUrl.contains("/flow.php")){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.setFragmentIndex(3);
                }
            });
        }else if (lowerUrl.contains("/user.php")){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.setFragmentIndex(4);
                }
            });
        }else if (lowerUrl.contains("/lmshop.php")){
            Map map = Utils.URLRequest(url);
            if (map == null || !map.containsKey("act") || map.get("act").toString().contains("shop_index")){
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        activity.setFragmentIndex(2);
                    }
                });
            }else {
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url = datas.get(i).getMenu_url();
                        new CheckPresenter(GvSubjectAdapter.this).checkKey(context,url);
                    }
                });
            }
        }else if(lowerUrl.contains("/category.php?")){
            view.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
//                    Intent intentjs=new Intent(context, WebActivity.class);
//                    intentjs.putExtra("url", ConfigValue.IMG_IP + datas.get(i).getMenu_url() + "&from_app=2&key="+ConfigValue.DATA_KEY + "&rid=" + ConfigValue.rid);
//                    intentjs.putExtra("title",datas.get(i).getMenu_name());
//                    intentjs.putExtra("type","1");
//                    context.startActivity(intentjs);
                    Intent mIntent = new Intent(context,GoodsFilterActivity.class);
                    Map map = Utils.URLRequest(datas.get(i).getMenu_url());
                    mIntent.putExtra("id",map.get("id").toString());
                    context.startActivity(mIntent);
                }
            });

        }else {
            view.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    String url="";
                    if(datas.get(i).getMenu_url().indexOf("?")>0)
                    {
                        url= ConfigValue.IMG_IP+datas.get(i).getMenu_url()+"&from_app=2&key="+ConfigValue.DATA_KEY + "&rid=" + ConfigValue.rid;
                    }else {
                        url= ConfigValue.IMG_IP+datas.get(i).getMenu_url()+"?from_app=2&key="+ConfigValue.DATA_KEY + "&rid=" + ConfigValue.rid;
                    }
                    Intent intentjs=new Intent(context, WebActivity.class);
                    intentjs.putExtra("url", url);
                    intentjs.putExtra("type","2");
                    context.startActivity(intentjs);
                }
            });
        }


        return view;
    }

    @Override
    public void getData(BaseModel response, String url) {
        if (response.getCode() == null)
            return;
        if (Integer.parseInt(response.getCode()) < 0){
            ConfigValue.DATA_KEY = "";
        }

        Intent intentjs= null;
        if (type == TYPE_HOME){
            String lowerUrl = url.toLowerCase();
            if (lowerUrl.contains("/lmshop.php")){
                intentjs = new Intent(context, JsWebActivity.class);
                intentjs.putExtra("url", url);
                ((MainActivity)context).startActivity(intentjs);
                return;
            }
            intentjs = new Intent(context, WebActivity.class);
            intentjs.putExtra("url", url);
            intentjs.putExtra("type","2");
            context.startActivity(intentjs);
        }else{
            intentjs = new Intent(context, JsWebActivity.class);
            intentjs.putExtra("url", url);
            ((MainActivity)context).startActivityForResult(intentjs,MainActivity.RESULT_NEAR);
        }

    }

    class ViewHolder{
        private ImageView iv_icon;
        private TextView tv_info;

        public ViewHolder(View view){
            iv_icon= (ImageView) view.findViewById(R.id.iv_icon);
            tv_info= (TextView) view.findViewById(R.id.tv_info);
        }

        /**
         * 绑定数据
         */
        public void bindData(Context context, String src, String content) {
            Picasso.with(context).load(src).into(iv_icon);
            tv_info.setText(content);
        }
    }
}
